<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="车间名称" prop="workShopName">
        <el-input
          v-model="queryParams.workShopName"
          placeholder="请输入车间名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="生产组别" prop="productGroup">
        <el-input
          v-model="queryParams.productGroup"
          placeholder="请输入车间名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工单编号" prop="workPlanId">
        <el-input
          v-model="queryParams.workPlanId"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="客户名称" prop="customerName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.customerName"-->
      <!--          placeholder="请输入客户名称"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="客户料号" prop="customerProductId">
        <el-input
          v-model="queryParams.customerProductId"
          placeholder="请输入客户料号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="产品机种" prop="machineType">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.machineType"-->
      <!--          placeholder="请输入产品机种"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->

      <!--      </el-form-item>-->
      <el-form-item label="机种件别" prop="machineItem">
        <el-select
          v-model="queryParams.machineItem"
          placeholder="请选择机种件别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_MACHINE_ITEM)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单类型" prop="workPlanTypeCode">
        <el-select
          v-model="queryParams.workPlanTypeCode"
          placeholder="请选择工单类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_WORK_PLAN_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择工单状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_WORK_PLAN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="切换方式" prop="exchangeType">
        <el-select
          v-model="queryParams.exchangeType"
          placeholder="请选择切换方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_EXCHANGE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模具共用" prop="isMoldSame">
        <el-select
          v-model="queryParams.isMoldSame"
          placeholder="请选择是否模具共用"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计划开始" prop="planStartTime">
        <el-date-picker
          v-model="queryParams.planStartTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="计划结束" prop="planEndTime">
        <el-date-picker
          v-model="queryParams.planEndTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px"/>
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['mes:base-work-plan:create']"
        >
          <Icon icon="ep:plus" class="mr-5px"/>
          新增
        </el-button>
        <el-button
          type="warning"
          plain
          @click="openForm('create')"
          v-hasPermi="['mes:base-work-plan:create']"
        >
          <Icon icon="ep:refresh" class="mr-5px"/>
          同步
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mes:base-work-plan:export']"
        >
          <Icon icon="ep:download" class="mr-5px"/>
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <el-affix :offset="100">
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        highlight-current-row
        @current-change="handleCurrentChange"
      >
        <el-table-column label="工单编号" align="center" prop="workPlanId" min-width="180" fixed/>
        <el-table-column label="工单状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.MES_WORK_PLAN_STATUS"
                      :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="切换方式" align="center" prop="exchangeType">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.MES_EXCHANGE_TYPE"
                      :value="scope.row.exchangeType"/>
          </template>
        </el-table-column>
        <el-table-column label="工单类型" align="center" prop="workPlanTypeCode" min-width="100px">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.MES_WORK_PLAN_TYPE" :value="scope.row.workPlanTypeCode"/>
          </template>
        </el-table-column>
        <el-table-column label="生产车间" align="center" prop="workShopName" min-width="120"/>
        <el-table-column label="生产组别" align="center" prop="productGroup" min-width="120"/>
        <el-table-column label="机台名称" align="center" prop="machineName" min-width="120"/>
        <el-table-column
          label="计划开始时间"
          align="center"
          prop="planStartTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column
          label="计划结束时间"
          align="center"
          prop="planEndTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column label="产品料号" align="center" prop="productId" min-width="180"/>
        <el-table-column label="产品名称" align="center" prop="productName" min-width="200"/>
        <el-table-column label="模具编号" align="center" prop="moldNo" min-width="180px"/>
        <el-table-column label="模穴数量" align="center" prop="moldCaveNum"/>

        <el-table-column label="是否模具共用" align="center" prop="isMoldSame" min-width="120px">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isMoldSame+''"/>
          </template>
        </el-table-column>
        <el-table-column label="包规数量" align="center" prop="quantity"/>
        <!--      <el-table-column label="客户ID" align="center" prop="customerId" />-->
        <!--      <el-table-column label="客户名称" align="center" prop="customerName" />-->
        <!--      <el-table-column label="计量单位名称" align="center" prop="unitName" />-->
        <!--      <el-table-column label="计量单位编号" align="center" prop="unitId" />-->
        <el-table-column label="客户料号" align="center" prop="customerProductId" min-width="200"/>
        <!--      <el-table-column label="产品机种" align="center" prop="machineType" />-->
        <el-table-column label="机种件别" align="center" prop="machineItem">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.MES_MACHINE_ITEM"
                      :value="scope.row.machineItem"/>
          </template>
        </el-table-column>
        <el-table-column label="产品简称" align="center" prop="productShortName" min-width="180"/>
        <!--      <el-table-column label="生产组编码" align="center" prop="productGroupCode" min-width="100px" />-->

        <!--      <el-table-column label="工单类型编码" align="center" prop="workPlanTypeCode" min-width="100px" />-->
        <!--      <el-table-column label="车间ID" align="center" prop="workShopId" />-->

        <!--      <el-table-column label="车间名称编码" align="center" prop="workShopCode" />-->
        <el-table-column label="计划产量" align="center" prop="workPlanProductNum" min-width="100">
          <template #default="scope">
            {{ scope.row.workPlanProductNum }} {{ scope.row.unitName }}
          </template>
        </el-table-column>

        <!--      <el-table-column-->
        <!--        label="实际开始时间"-->
        <!--        align="center"-->
        <!--        prop="actualStartTime"-->
        <!--        :formatter="dateFormatter"-->
        <!--        width="180px"-->
        <!--      />-->
        <!--      <el-table-column-->
        <!--        label="实际结束时间"-->
        <!--        align="center"-->
        <!--        prop="actualEndTime"-->
        <!--        :formatter="dateFormatter"-->
        <!--        width="180px"-->
        <!--      />-->

        <!--      <el-table-column label="发料人编号" align="center" prop="distributeManagerId" />-->
        <!--      <el-table-column label="收料人编号" align="center" prop="receiveManagerId" />-->

        <el-table-column
          label="制单时间"
          align="center"
          prop="generateDateTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column label="制单人" align="center" prop="generateUser"/>
        <el-table-column
          label="结案时间"
          align="center"
          prop="closeDateTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <!--      <el-table-column label="工单内码" align="center" prop="workPlanInterId" />-->

        <el-table-column label="操作" align="center" min-width="120px" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['mes:base-work-plan:update']"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['mes:base-work-plan:delete']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        :small="true"
        :page-sizes="[5,10,15,20, 30, 50, 100]"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </el-affix>

  <!-- 表单弹窗：添加/修改 -->
  <BaseWorkPlanForm ref="formRef" @success="getList"/>
  <!-- 子表的列表 -->
  <ContentWrap>
    <el-tabs model-value="baseBomChild">

      <el-tab-pane label="投料单详细信息" name="baseBomChild">
        <BaseBomChildList :work-plan-inter-id="currentRow.workPlanInterId"/>
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
</template>

<script setup lang="ts">
import {getStrDictOptions, DICT_TYPE} from '@/utils/dict'
import {dateFormatter} from '@/utils/formatTime'
import download from '@/utils/download'
import {BaseWorkPlanApi, BaseWorkPlanVO} from '@/api/mes/baseworkplan'
import BaseWorkPlanForm from './BaseWorkPlanForm.vue'
import BaseBomChildList from './components/BaseBomChildList.vue'

/** 工单信息 列表 */
defineOptions({name: 'BaseWorkPlan'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BaseWorkPlanVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 5,
  workPlanId: undefined,
  productName: undefined,
  customerName: undefined,
  customerProductId: undefined,
  machineType: undefined,
  machineItem: undefined,
  workPlanType: undefined,
  workPlanTypeCode: undefined,
  workShopName: undefined,
  planStartTime: [],
  planEndTime: [],
  status: undefined,
  exchangeType: undefined,
  isMoldSame: undefined,
  productGroup: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BaseWorkPlanApi.getBaseWorkPlanPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BaseWorkPlanApi.deleteBaseWorkPlan(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await BaseWorkPlanApi.exportBaseWorkPlan(queryParams)
    download.excel(data, '工单信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
